<template>
  <div class="employees">
    <el-card class="top">
      <div class="btn">
        <el-button v-print="{ id: printarea }" type="primary">打印</el-button>
        <el-button
          v-buttonShow="'employees_importBtn'"
          type="primary"
          @click="outputClick"
        >导出</el-button>
        <el-button
          v-buttonShow="'employees_outputBtn'"
          type="primary"
          @click="importClick"
        >导入</el-button>
        <el-button type="primary" @click="addEvent">新增员工</el-button>
      </div>
    </el-card>

    <el-card>
      <el-table :data="list" style="width: 100%">
        <el-table-column label="序号" type="index">
          <template v-slot="{ $index }">
            <div>
              {{ (page.page - 1) * page.size + $index + 1 }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="姓名" sortable prop="username" />
        <el-table-column label="头像" sortable prop="staffPhoto">
          <template v-slot="{ row }">
            <img
              v-Imgerror="defaultImg"
              :src="row.staffPhoto"
              class="avatar"
              @click="qrcodeshow(row.staffPhoto)"
            >
          </template>
        </el-table-column>
        <el-table-column label="手机号" sortable prop="mobile" />
        <el-table-column label="工号" sortable prop="workNumber" />
        <el-table-column label="聘用形式" sortable prop="formOfEmployment" />
        <!-- :formatter="formatter" -->
        <el-table-column label="部门" sortable prop="departmentName" />

        <el-table-column label="入职时间" prop="timeOfEntry" />
        <el-table-column
          label="是否在职"
          prop="enableState"
          :formatter="formatter"
        />

        <el-table-column label="操作" sortable width="300" fixed="right">
          <template v-slot="{ row }">
            <div>
              <el-button type="text" @click="getDetail(row.id)">查看</el-button>
              <el-button type="text">转正</el-button>
              <el-button type="text">调岗</el-button>
              <el-button type="text">离职</el-button>
              <el-button type="text" @click="RoleClick(row.id)">角色</el-button>
              <el-button type="text" @click="del(row.id)">删除</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <div class="page">
        <el-pagination
          :current-page="page.page"
          :page-size="page.size"
          layout="total,prev,pager,next,jumper,sizes"
          :total="page.total"
          :page-sizes="[1, 5, 10, 20, 50]"
          @size-change="sizeChange"
          @current-change="pageChange"
        />
      </div>
    </el-card>
    <Add ref="add" @getDate="getData" />
    <el-dialog title="二维码" :visible.sync="isShow" width="500px">
      <canvas ref="canvas" />
    </el-dialog>
    <Role ref="role" :is-show.sync="isShow" />
  </div>
</template>

<script>
import moment from 'moment'
import Add from './compuments/add.vue'
import { sysUser, sysUserDelete } from '@/api/human.js'
import employeesData from '@/api/constant/employees'
import cookieJs from 'js-cookie'
import Qrcode from 'qrcode'
import Role from './compuments/Role.vue'

export default {
  components: {
    Add,
    Role
  },
  data() {
    return {
      list: [{}],
      isShow: false,
      page: {
        total: 50,
        size: +cookieJs.get('size') || 5,
        page: 1
      },
      defaultImg: require('@/assets/common/head.jpg')
    }
  },
  created() {
    this.getData()
  },

  methods: {
    // 获取数据
    async getData() {
      const res = await sysUser(this.page)
      res.data.rows.forEach((item) => {
        const result = employeesData.hireType.find((item2) => {
          return item2.id === +item.forOfEmployment
        })
        const resultStr = result ? result.value : '-'
        item.forOfEmployment = resultStr
        item.timeOfEntry = moment(item.timeOfEntry).format('YYYY-MM-DD')
      })
      //

      this.list = res.data.rows
      this.page.total = res.data.total
      console.log('员工列表', res)
    },
    // 页数规模转换
    sizeChange(size) {
      this.page.size = size
      cookieJs.set('size', size)
      this.page.page = 1
      this.getData()
    },
    // 换页
    pageChange(page) {
      this.page.page = page
      this.getData()
    },
    // 添加事件
    addEvent() {
      this.$refs.add.isShow = true
    },
    // 聘用形式转换--组件自带转换
    formatter(row, column, cellValue, index) {
      const result = employeesData.workingState.find((item) => {
        return +item.id === cellValue
      })
      return result ? result.value : '-'
    },
    // 导出框
    async outputClick() {
      const header = {
        姓名: 'username',
        手机号: 'mobile',
        入职时间: 'timeOfEntry',
        聘用形式: 'formOfEmployment',
        工号: 'workNumber',
        组织名称: 'departmentName',
        转正时间: 'correctionTime'
      }
      const res = await sysUser({
        page: 1,
        size: 10000
      })

      const data = res.data.rows.map((item) => {
        return Object.keys(header).map((item2) => {
          return item[header[item2]]
        })
      })
      import('@/vendor/Export2Excel')
        .then((res) => {
          res.export_json_to_excel({
            header: Object.keys(header),
            data,
            filename: '用户列表'
          })
          console.log(res)
        })
        .catch(() => {})
    },
    // 删除用户数据
    del(id) {
      this.$confirm('你确定要删除该用户数据吗', 'Tips')
        .then(async(result) => {
          await sysUserDelete(id)
          this.$message.success('删除成功')
          this.pageChange(1)
        })
        .catch(() => {})
    },
    // 跳转到导入页面
    importClick() {
      this.$router.push('/human/import')
    },
    getDetail(id) {
      this.$router.push('/human/detail/' + id)
    },
    // 弹出二维码
    qrcodeshow(url) {
      if (!url) {
        return
      }
      this.isShow = true
      this.$nextTick(() => {
        Qrcode.toCanvas(this.$refs.canvas, url, {
          width: 300,
          height: 300
        })
      })
    },
    // 角色权限管理
    RoleClick(id) {
      this.isShow = true
      this.$refs.role.getUserInfo(id)
    }
  }
  // 此处有4种数据转换方式
  //  1element-ul提供的转换方式
  //  2使用过滤器进行转换
  //  3使用自定义指令进行转换
  //  4先转换再进行渲染 该方法不算 本方法考虑了节省性能
  //   formatter(row, column, cellValue, index) {
  //     const result = employeesData.hireType.find((item) => {
  //       return item.id === cellValue
  //     })
  //     return result ? result.value : '-'
  //   }
  // }
}
</script>

<style lang="scss" scoped>
.employees {
  padding: 20px;
  .top {
    margin-bottom: 15px;
    .btn {
      text-align: right;
    }
  }
  .page {
    padding-top: 15px;
    text-align: right;
  }
  .avatar {
    width: 70px;
    height: 70px;
  }
}
</style>
